var Promise = require('libs/es6-promise.min.js') 
//获取指定长度的随机字符串
function getRandomString(len) {
  　　len = len || 32;
  　　var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  　　var maxPos = $chars.length;
  　　var pwd = '';
  　　for (i = 0; i < len; i++) {
    　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  　　}
  　　return pwd;
}

//验证手机号
function validateMobile(mobile) {
  if (mobile.length == 0) {
    wx.showToast({
      title: '请输入手机号码',
    })
    // document.form1.mobile.focus();
    return false;
  }
  if (mobile.length != 11) {
    wx.showToast({
      title: '请输入有效的手机号码',
    })
    return false;
  }
  console.log("validateMobile param: " + mobile);
  var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
  // var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
  if (!myreg.test(mobile)) {
    wx.showToast({
      title: '请输入有效的手机号码',
    })
    return false;
  }
  return true;
}

function trim(str, char, typ) {
  if (char) {
    if (typ == 'left') {
      return str.replace(new RegExp('^\\' + char + '+', 'g'), '');
    } else if (typ == 'right') {
      return str.replace(new RegExp('\\' + char + '+$', 'g'), '');
    }
    return str.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
  }
  return str.replace(/^\s+|\s+$/g, '');
};

function showToast(title){
  wx.showToast({
    title: title,
  })
}
function arrContains(arr, obj) {
  var i = arr.length;
  while (i--) {
    if (JSON.stringify(arr[i]) == JSON.stringify(obj)) {
      return true;
    }
  }
  return false;
}  
function getLocalTime(nS) {
  return new Date(parseInt(nS)).toLocaleString();//.replace(/年|月/g, "-").replace(/日/g, " ");
}     

function add0(m) { return m < 10 ? '0' + m : m }
function date(ts) {
  //shijianchuo是整数，否则要parseInt转换
  var time = new Date(ts);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d);// + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}
function dateTime(ts) {
  //shijianchuo是整数，否则要parseInt转换
  var time = new Date(ts);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  var str = y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
  
  return str;
}

function isEmpty(obj){
  if (obj == null || obj == "" || typeof (obj) == undefined || JSON.stringify(obj) == '{}'){
    return true;
  }
  return false;
}

function isFromInfoEmpty(obj) {
  if (obj == null || typeof (obj) == undefined || JSON.stringify(obj) == '{}' || this.isEmpty(obj.cityId) || this.isEmpty(obj.position) || this.isEmpty(obj.cityName) || this.isEmpty(obj.airportId)) {
    return true;
  }
  return false;
}

function toRad(d) { return d * Math.PI / 180; }
function getDisance(lat1, lng1, lat2, lng2) {
  // #lat为纬度, lng为经度, 一定不要弄错
  var dis = 0;
  var radLat1 = toRad(lat1);
  var radLat2 = toRad(lat2);
  var deltaLat = radLat1 - radLat2;
  var deltaLng = toRad(lng1) - toRad(lng2);
  var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
  return dis * 6378137;
} 


function wxPromisify(fn) { 
  return function (obj = {}) { 
    return new Promise((resolve, reject) => { obj.success = function (res) { resolve(res) } 
    obj.fail = function (res) { reject(res) } 
    fn(obj) 
    }) } 
  }


module.exports = {
  getRandomString: getRandomString,
  validateMobile: validateMobile,
  trim: trim,
  arrContains: arrContains,
  getLocalTime: getLocalTime,
  date: date,
  dateTime: dateTime,
  isEmpty: isEmpty,
  isFromInfoEmpty: isFromInfoEmpty,
  wxPromisify: wxPromisify,
  getDisance: getDisance
}